package com.nmsl.system.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class OSSUtil {
    // yourEndpoint,填写Bucket所在地域对应的Endpoint。
    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    // 填写Bucket名称。
    private static final String bucketName = "liujinpingfile";
    // 填写Object的完整路径。Object完整路径中不能包含Bucket名称。
    private static final String objectName = "one/";

    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。
    private static final String accessKeyId = "LTAI5tEkwuuK71MMKwGPc3cx";
    private static final String accessKeySecret = "SdYbvnT2n5GWwnPclHoTN6jvy5aLO5";

    // 文件url前缀
    private static final String furl = "https://liujinpingfile.oss-cn-beijing.aliyuncs.com/";

    // 文件上传方法
    public  static   String   upload(String fileName, InputStream in){
        // 组合objectName
        String objectNameUrl = objectName + fileName;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传
        PutObjectResult re = ossClient.putObject(bucketName, objectNameUrl, in);
        //关闭连接
        ossClient.shutdown();

        if(re != null&&re.getETag() != null){
            return furl+objectName+fileName;
        }
        return null;
    }

    //文件下载方法
    public  static   void   download(String fileName, OutputStream out) throws IOException {
        // 组合objectName
        String objectNameUrl = objectName + fileName;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 下载
        OSSObject object = ossClient.getObject(bucketName, objectNameUrl);
        InputStream in = object.getObjectContent();

        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = in.read(bytes))!= -1){
            out.write(bytes,0,len);
        }
        out.close();
        in.close();

        //关闭连接
        ossClient.shutdown();
    }
    // 删除字符串前缀的方法
    private static String removePrefix(String str, String prefix) {
        if (str == null || prefix == null) {
            return str;
        }
        if (str.startsWith(prefix)) {
            return str.substring(prefix.length());
        }
        return str;
    }
    // 文件删除方法
    public static void delete(String fileName) {
        try {
            // 要删除的前缀
            String prefix = "https://liujinpingfile.oss-cn-beijing.aliyuncs.com/";
            // 删除前缀
            String objectNameUrl= removePrefix(fileName, prefix);
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            // 删除
            System.out.println(ossClient.deleteObject(bucketName, objectNameUrl));

            //关闭连接
            ossClient.shutdown();
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}